<?php
/**
* author: fengfangqian
* 版权： 拿钱来
* 无限分类
*/
class SortClass{

    var $data = array();
    var $key = array();
    var $remark = array();
    var $child = array(-1=>array());
    var $layer = array(-1=>-1);
    var $parent = array();
    var $db;
    var $datatable="wechatbox_admin.t_reported_sys";
    var $showhtml;
    function SortClass($db){
            #if(!FunctionUtils::deny()){die(@FunctionUtils::emptyJSON("IP受限"));}
            $this->setNode(0, -1, '顶极节点','','');
            $this->db = $db;
            $node = array();
            $results = $db->query('select * from '.$this->datatable.' order by sys_pid');
            while($node = $db->fetch_array($results)){
                    $this->setNode($node['sys_id'],$node['sys_pid'],$node['sys_name'],$node['sys_key'],$node['sys_remark']);
            }
    }
    function setNode ($id, $parent, $value,$key,$remark){
            $parent = $parent?$parent:0;
            $this->data[$id] = $value;
            $this->key[$id] = $key;
            $this->remark[$id] = $remark;
            $this->child[$id] = array();
            $this->child[$parent][] = $id;
            $this->parent[$id] = $parent;
            $this->layer[$id] = !isset($this->layer[$parent])? 0 : $this->layer[$parent] + 1;
    }
    function getList (&$tree, $root= 0){
            foreach ($this->child[$root] as $key=>$id){
                    $tree[] = $id;
                    if ($this->child[$id]) $this->getList($tree, $id);
            }
    }
    function getValue ($id){return $this->data[$id];}
    function getKey ($id){return $this->key[$id];}
    function getRemark ($id){return $this->remark[$id];}
    function getLayer ($id, $space = false){
            return $space?str_repeat($space, $this->layer[$id]):$this->layer[$id];
    }
    function getParent ($id){return $this->parent[$id];}
    function getParents ($id){
            while ($this->parent[$id] != -1){
                    $id = $parent[$this->layer[$id]] = $this->parent[$id];
            }
            ksort($parent);
            reset($parent);
            return $parent;
    }
    function getChild ($id){return $this->child[$id];}
    function getChilds ($id = 0){
            $child = array($id);
            $this->getList($child, $id);
            return $child;
    }
    ///***以下为现实列表逻辑函数////
    function showt_weixin_cate_list($id) {
        $this->_wexin_cate_list($this->getChild($id));
        $this->showhtml='<dl class="clearfix">'.$this->showhtml.'</dl>';
    }
    private function _wexin_cate_list($category){//本函数支持无限极,但是重构结构只支持两级
        $this->showhtml.='';
        foreach ($category as $id){
            $childs=$this->getChild($id);
            $parentid=$this->getParent($id);
            //if(in_array($parentid,$category)){continue;}//防止重复输出
            $havechild=empty($childs)?false:true;
            if($havechild){
                $this->showhtml.= '<dt>['.$this->getValue($id).']</dt><dd>';
                $this->_wexin_cate_list($childs);
            }else{
                $this->showhtml.= '<a href="cate.php?cid='.urlencode($this->getValue($id)).'">'.$this->getValue($id).'</a>';
            }
        }
        $this->showhtml.="</dd>";

    }
    
    function show_menu_cate_list($id){//显示菜单项分类
        $this->_menu_cate_list($this->getChild($id));
    }
    private function _menu_cate_list($category){//本函数支持二级分类,多了会破相的
        $this->showhtml.='<div class="sort-down">';
        foreach ($category as $id){
            $childs=$this->getChild($id);
            $parentid=$this->getParent($id);
            //if(in_array($parentid,$category)){continue;}//防止重复输出
            $havechild=empty($childs)?false:true;
            
            if($havechild){
                $this->showhtml.= '<div class="sort-div"><h3>'.$this->getValue($id).'</h3><ul>';
                 foreach($childs as $child){
                     $this->showhtml.= '<li><a href="cate.php?cid='.urlencode($this->getValue($child)).'">'.$this->getValue($child).'</a></li>';
                 }
                 $this->showhtml.='</ul></div>';
            }else{
                $this->showhtml.= '<div class="sort-div"><h3>'.$this->getValue($id).'</h3></div>';
            }
        }
        $this->showhtml.="</div>";

    }
    function showt_weixin_area_list($id){//显示菜单项分类
        $category=$this->getChild($id);
        $this->showhtml='<div class="area"><ul>';
        if(!$category){
            return false;
        }else{
            foreach ($category as $id){
                $this->showhtml.='<li><a href="cate.php?area='.urlencode($this->getValue($id)).'">'.$this->getValue($id).'</a></li>';
            }
        }
        $this->showhtml.='</ul></div>';
    }
    function show_industry_for_js($id){//生成行业分类数据,供industry.js调用,仅支持二级
//        IPCAD = "#一级$二级|二级#一级$二级|二级";
        $this->showhtml='';
        $category=$this->getChild($id);
        foreach ($category as $id){
            $childs=$this->getChild($id);
            $parentid=$this->getParent($id);
            $havechild=empty($childs)?false:true;
            $this->showhtml=rtrim($this->showhtml,"|")."#".$this->getValue($id).'$';
            foreach($childs as $child){
                 $this->showhtml.= $this->getValue($child).'|';
            }
        }
        $this->showhtml=ltrim(rtrim($this->showhtml,"|"),"#");
    }
}
